package org.qydjk.console.controller.sys;

import org.apache.tools.ant.util.CollectionUtils;
import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.SRole;
import org.qydjk.common.pojo.SUserRole;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.common.vo.Tree;
import org.qydjk.console.constant.ConsoleConstant;
import org.qydjk.console.service.ISysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class SRoleController extends BaseController {
    @Autowired
    private ISysService sysService;
    /**
     * “角色管理”入口
     * @return
     */
    @RequestMapping("/manager.do")
    public String roleManager(){
        return "admin/role/role";
    }

    /**
     * 权限列表
     *
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @PostMapping("/dataGrid.do")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order) throws Exception {
        HibernatePage pager = new HibernatePage(page,rows);
        String hql = "from SRole sr order by sr." + sort +  " " + order;
        List<SRole> list = baseService.findByHql(hql, pager);
        int total = baseService.getRecordCountHQL("select count(sr.id) from SRole sr");
        return new EasyUIResponse(total,list);
    }

    /**
     * 权限树
     *
     * @return
     */
    @PostMapping("/tree.do")
    @ResponseBody
    public Object tree() throws Exception {
        List<Tree> trees = new ArrayList<Tree>();
        List<SRole> roles = baseService.findByHql("from SRole sr order by sr.seq");
        for (SRole role : roles) {
            Tree tree = new Tree();
            tree.setId(role.getId());
            tree.setText(role.getName());

            trees.add(tree);
        }
        return trees;
    }

    /**
     * 添加权限页
     *
     * @return
     */
    @GetMapping("/addPage.do")
    public String addPage() {
        return "admin/role/roleAdd";
    }

    /**
     * 添加权限
     *
     * @param role
     * @return
     */
    @PostMapping("/add.do")
    @ResponseBody
    public Object add(@Valid SRole role) throws Exception {
        baseService.save(role);
        return renderSuccess("添加成功！");
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object delete(Integer id){
        try {
            int ct = baseService.getRecordCountHQL("select count(sur.id) from SUserRole sur where sur.roleId = ?", id.intValue());
            if(ct > 0){
                return renderError("有用户关联，无法删除");
            }
            SRole role = baseService.get(SRole.class, id);
            if(role.getName().equals(ConsoleConstant.ADMINISTRATOR_ROLE_NAME)){
                return renderError("超级管理员不允许删除");
            }
            baseService.delete(role);
            return renderSuccess("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return renderSuccess("删除失败！");
        }
    }

    /**
     * 编辑权限页
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage.do")
    public String editPage(Model model, Integer id) throws Exception {
        SRole role = baseService.get(SRole.class,id);
        model.addAttribute("role", role);
        return "admin/role/roleEdit";
    }

    /**
     * 删除权限
     *
     * @param role
     * @return
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object edit(@Valid SRole role) throws Exception {
        baseService.update(role);
        return renderSuccess("编辑成功！");
    }

    /**
     * 授权页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/grantPage.do")
    public String grantPage(Model model, Integer id) {
        model.addAttribute("id", id);
        return "admin/role/roleGrant";
    }

    /**
     * 授权页面页面根据角色查询资源
     *
     * @param id
     * @return
     */
    @RequestMapping("/findResourceIdListByRoleId.do")
    @ResponseBody
    public Object findResourceByRoleId(Integer id) throws Exception {
        List<Object> resources = baseService.findSingleColumnListBySQL("select sre.resource_id from s_role sr left join s_role_resource sre on sr.id = sre.role_id where sr.id = ?",id);
        return renderSuccess(resources);
    }

    /**
     * 授权
     *
     * @param id
     * @param resourceIds
     * @return
     */
    @RequestMapping("/grant.do")
    @ResponseBody
    public Object grant(Integer id, String resourceIds) throws Exception {
        sysService.updateRoleResource(id, resourceIds);
        return renderSuccess("授权成功！");
    }
}
